<?php
/*
 * @Date: 2021-01-28 17:50:02
 * @名称: 漫画 · 操作控制器 · 增加漫画章节
 * @版本: 0.01
 * @作者: 初雪桜
 * @邮箱: 202184199@qq.com
 * @最后编辑: 初雪桜
 * @LastEditTime: 2021-03-01 22:35:22
 * @FilePath: /Site/app/useradmin/controller/Comics/Operation/ComicsAddProject.php
 */

namespace app\useradmin\controller\Comics\Operation;


use app\BaseController;
use think\facade\Db;

class ComicsAddProject extends BaseController
{
    /**
     * @方法名: 增加分卷
     * @作者: 初雪桜
     * @信息: 后台->漫画列表->增加分卷
     * @param {Json} $user_info 用户信息
     * @param {String} $bookId 书籍id
     * @param {*String} $projectName 分卷名称
     * @param {String} $order 序列
     * @return {json}
     */
    public function AddProject($user_info, $bookId, $projectName, $order)
    {

        if ((float) $order == 0) {
            return errorJsonReturn('Missing parameter', '序号不正确');
        }

        $MyComicsState = Db::table('book_info')
            ->alias('book_info')
            ->where([
                "book_info._id" => $bookId,
                "author_and_chinese_team.user_id" => $user_info['_id']
            ])
            ->join('author_and_chinese_team author_and_chinese_team', 'author_and_chinese_team.book_id = book_info._id')
            ->order('author_and_chinese_team.id desc')
            ->field([
                "author_and_chinese_team.edit_delete_user",
                #允许删除用户
                "author_and_chinese_team.edit_inherit_users",
                #是否允许继承用户
                "author_and_chinese_team.edit_add_user",
                #增加用户
                "author_and_chinese_team.edit_edit_user",
                #编辑用户
                "author_and_chinese_team.edit_sever_comics_info",
                #是否允许保存修改漫画信息
                "author_and_chinese_team.edit_add_project",
                #是否允许添加章节
                #1允许添加 0不允许
                "author_and_chinese_team.state",
            ])
            ->find();
        if (!$MyComicsState) {
            //判断我的状态
            return errorJsonReturn('User permission error');
        }
        if ($MyComicsState['state'] != 0 || $MyComicsState['edit_add_project'] != 1) {
            //判断我的权限
            return errorJsonReturn('Insufficient user rights');
        }

        $insertID = randIdStructure(5);
        $selectbook_info_project = Db::table('book_info_project')
            ->where([
                '_id' => $bookId,
                #书籍id
                'order' => $order,
                #分卷序列号
            ])
            ->count();
        if ($selectbook_info_project) {
            //判断要插入的分卷id是否存在
            return errorJsonReturn('Duplicate data already exists');
        }
        Db::table("book_info_project")
            #插入分类列表
            ->insert([
                '_id' => $bookId,
                #书籍id
                'project_title' => $projectName,
                #分卷名
                'order' => $order,
                #分卷序列号
                "updated_at" => date('Y-m-d H:i:s'),
                #发布时间
                "chapter_id" => $insertID,
                #分卷id
            ]);
        // 插入操作日志
        Db::table('log_book_info_project')
            ->insert([
                "_id" => $bookId,
                #书籍id
                "project_id" => $insertID,
                #分卷id 用于记录哪一个操作的
                "project_name" => $projectName,
                #分卷名
                "project_order" => $order,
                "msg" => "用户$user_info[_id]创建了此分卷",
            ]);
        return successJsonReturn([
            "bookId" => $bookId,
            "chapter_id" => $insertID,
            "title" => $projectName,
            "order" => $order,
            // "insert_user_id" => $user_info["_id"],
        ]);
    }
}
